#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <zmq.h>

#define IPC_ADDRESS "ipc:///var/tmp/asset_data.socket"
#define BUFFER_SIZE 8192

int main() {
    // 创建 ZMQ 上下文和套接字
    void *context = zmq_ctx_new();
    void *puller = zmq_socket(context, ZMQ_PULL);
    if (!puller) {
        perror("zmq_socket failed");
        return -1;
    }

    // 连接 IPC 地址
    // int rc = zmq_connect(puller, IPC_ADDRESS);
    // if (rc != 0) {
    //     perror("zmq_connect failed");
    //     zmq_close(puller);
    //     zmq_ctx_destroy(context);
    //     return -1;
    // }

    // 绑定 IPC 地址
    int rc = zmq_bind(puller, IPC_ADDRESS);
    if (rc != 0) {
        perror("zmq_bind failed");
        zmq_close(puller);
        zmq_ctx_destroy(context);
        return -1;
    }

    printf("Puller started. Listening on %s...\n", IPC_ADDRESS);

    char buffer[BUFFER_SIZE];
    
    while (1) {
        // 接收消息
        int bytes = zmq_recv(puller, buffer, BUFFER_SIZE-1, 0);
        if (bytes < 0) {
            perror("zmq_recv failed");
            break;
        }
        
        buffer[bytes] = '\0';  // 添加字符串终止符
        printf("Received: %s\n", buffer);
    }

    // 清理资源
    zmq_close(puller);
    zmq_ctx_destroy(context);
    return 0;
}
